package cn.layfolk.daimao.config;

import cn.hutool.json.JSONUtil;
import cn.layfolk.daimao.vo.respvo.ResponseVo;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.AccessDeniedException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.stereotype.Component;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 * 描述: 权限不足异常类
 * 作者: panhongtong
 * 创建时间: 2020-12-03 10:40
 **/
@Component
public class CustomAccessDeniedHandler implements AccessDeniedHandler {

    @Override
    public void handle(HttpServletRequest httpServletRequest, HttpServletResponse response, AccessDeniedException e) throws IOException, ServletException {
        response.setStatus(HttpStatus.OK.value());
        response.setHeader("Content-Type", "application/json;charset=UTF-8");
        ResponseVo responseVo = new ResponseVo();
        responseVo.setStatusCode(401);
        // 权限不足
        responseVo.setMessage("权限不足！");
        response.getWriter().write(JSONUtil.toJsonStr(responseVo));
    }
}
